﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using RegisterationCommon.Common;
using RegisterationCommon.School;
using RegisterationCommon.Education;

namespace RegisterationSystem.Space_Education
{
    public partial class accounts_manage : System.Web.UI.Page
    {
        public string ShcJson = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ShcJson = BaseLibrary.GetSclJson();
                Bind();
                switch (Request.Form["action"])
                {
                    case "del":
                        DelSchoolacc();
                        break;
                    case "upd":
                        UpdSchoolacc();
                        break;
                    case "delacc":
                        DelSchoolaccByID();
                        break;
                    case "pswdreset":
                        UpdSchoolPswdReset();
                        break;

                }
            }
        }
        // 根据ID删除学校帐号
        void DelSchoolaccByID()
        {
            string id = Request.Form["id"];
            SchoolAccount s = new SchoolAccount();
            s.ID = Convert.ToInt32(id);
            bool flag = s.DelSchoolAccount();
            if (flag)
            {
                EduAccount.AddEduSysLog(Cookies.Get("AccountName").ToString(), DateTime.Now, HttpContext.Current.Request.UserHostAddress, "学校账号删除", "学校账号删除成功");
            }
            else
            {
                EduAccount.AddEduSysLog(Cookies.Get("AccountName").ToString(), DateTime.Now, HttpContext.Current.Request.UserHostAddress, "学校账号删除", "学校账号删除失败");
            }
        }
        //修改帐号状态
        void UpdSchoolacc()
        {
            string id = Request.Form["id"];
            string status = Request.Form["status"];
            SchoolAccount s = new SchoolAccount();
            s.ID = Convert.ToInt32(id);
            s.IsDisable = Convert.ToBoolean(Convert.ToInt32(status));
            bool flag = s.MdfSchoolAccountStatus();
            if (flag)
            {
                EduAccount.AddEduSysLog(Cookies.Get("AccountName").ToString(), DateTime.Now, HttpContext.Current.Request.UserHostAddress, "学校账号修改", "学校账号修改成功");
            }
            else
            {
                EduAccount.AddEduSysLog(Cookies.Get("AccountName").ToString(), DateTime.Now, HttpContext.Current.Request.UserHostAddress, "学校账号修改", "学校账号修改失败");
            }
        }
        //删除学校帐号
        void DelSchoolacc()
        {
            //获取删除学校帐号集合
            string idlist = Request.Form["id"];
            idlist = idlist.Substring(0, idlist.Length - 1);
            bool flag = DBAction.Delete("SchoolAccount", "id in (" + idlist + ")");
            if (flag)
            {
                EduAccount.AddEduSysLog(Cookies.Get("AccountName").ToString(), DateTime.Now, HttpContext.Current.Request.UserHostAddress, "学校账号删除", "学校账号删除成功");
            }
            else
            {
                EduAccount.AddEduSysLog(Cookies.Get("AccountName").ToString(), DateTime.Now, HttpContext.Current.Request.UserHostAddress, "学校账号删除", "学校账号删除失败");
            }
        }
        //重置帐号密码
        void UpdSchoolPswdReset()
        {
            string id = Request.Form["id"].ToString();
            string SchoolPswd = Request.Form["SchoolPswd"].ToString();
            SchoolAccount s = new SchoolAccount();
            s.ID = Convert.ToInt32(id);
            s.AccountPswd = BaseLibrary.ConvertToMD5(SchoolPswd.ToLower());
            bool flag = s.MdfSchoolAccountPswd();
            if (flag)
            {
                EduAccount.AddEduSysLog(Cookies.Get("AccountName").ToString(), DateTime.Now, HttpContext.Current.Request.UserHostAddress, "学校账号重置密码", "学校账号重置密码成功");
            }
            else
            {
                EduAccount.AddEduSysLog(Cookies.Get("AccountName").ToString(), DateTime.Now, HttpContext.Current.Request.UserHostAddress, "学校账号重置密码", "学校账号重置密码失败");
            }
        }
        //初始化学校下拉菜单
        void Bind()
        {
            DataTable dt = DBAction.GetDTFromSQL("select * from SchoolInfo");
            DataRow[] drs = dt.Select();
            foreach (DataRow dr in drs)
            {
                ListItem item = new ListItem();
                item.Text += dr["Name"].ToString();
                item.Value = dr["id"].ToString();
                ddlSchoolInfo.Items.Add(item);
            }
        }

        //添加
        protected void btnadd_Click(object sender, EventArgs e)
        {
            DataTable dt = DBAction.GetDTFromSQL("select * from SchoolAccount where AccountName='" + txtname.Value.Trim() + "'");
            if (dt.Rows.Count > 0)
            {
                //已登记
                Message.Send(10024);
                return;
            }
            SchoolAccount s = new SchoolAccount();
            s.AccountName = txtname.Value.Trim();
            s.AccountPswd = BaseLibrary.ConvertToMD5(txtpwd.Value.Trim().ToLower());
            s.IsDisable = ddlstatus.Value == "0" ? false : true;
            s.IsSuper = true;
            s.SchID = Convert.ToInt32(ddlSchoolInfo.SelectedValue);
            s.SuperAccount = 1;
            if (s.AddSchoolAccount())
            {
                Message.Send(10022);
                EduAccount.AddEduSysLog(Cookies.Get("AccountName").ToString(), DateTime.Now, HttpContext.Current.Request.UserHostAddress, "添加学校账号", "学校账号添加成功");
            }
            else
            {
                Message.Send(10023);
                EduAccount.AddEduSysLog(Cookies.Get("AccountName").ToString(), DateTime.Now, HttpContext.Current.Request.UserHostAddress, "添加学校账号", "学校账号添加失败");
            }
        }

    }
}